package com.zjq.springsocial.renren.api.impl.json;

import java.io.IOException;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.type.TypeReference;

import com.zjq.springsocial.renren.api.BasicInformation;
import com.zjq.springsocial.renren.api.Image;
import com.zjq.springsocial.renren.api.Like;
import com.zjq.springsocial.renren.api.School;
import com.zjq.springsocial.renren.api.User.EmotionalState;
import com.zjq.springsocial.renren.api.Work;

/**
 * 
 * <P>
 * Annotated mixin to add Jackson annotations to User
 * </P>
 * 
 * @author zhongjingquan
 * 
 * @version 0.0.1-SNAPSHOT 2013-7-18上午11:02:37
 */
@JsonIgnoreProperties(ignoreUnknown = true)
abstract class UserMixin {

	@JsonCreator
	UserMixin(
			@JsonProperty("id")	Long id, 
			@JsonProperty("name") String name, 
			@JsonProperty("Image") 
			@JsonDeserialize(using = ImageStateDeserializer.class)
			Image[] avatar, // 用户头像列表;
			@JsonProperty("star") Integer star // 是否为星级用户，值“1”表示“是”；值“0”表示“不是”
	) {}
	
	@JsonProperty("basicInformation")
	@JsonDeserialize(using = BasicInformationDeserializer.class)
	BasicInformation basicInformation;// 用户基本信息
	
	@JsonProperty("education")
	@JsonDeserialize(using = SchoolDeserializer.class)
	School[] education;// 用户学校信息
	
	@JsonProperty("work")
	@JsonDeserialize(using = WorkDeserializer.class)
	Work[] work;// 工作信息
	
	@JsonProperty("like")
	@JsonDeserialize(using = LikeDeserializer.class)
	Like[] like;// 喜欢
	
	@JsonProperty("emotionalState")
	@JsonDeserialize(using = EmotionalStateDeserializer.class)
	EmotionalState emotionalState;// 感情状态
	
	private static class EmotionalStateDeserializer extends JsonDeserializer<EmotionalState> {
		@Override
		public EmotionalState deserialize(JsonParser jp, DeserializationContext ctxt)
				throws IOException, JsonProcessingException {
			try {
				return EmotionalState.valueOf(jp.getText().toUpperCase());
			} catch (IllegalArgumentException e) {
				return EmotionalState.OTHER;
			}
		}
	}
	private static class ImageStateDeserializer extends JsonDeserializer<Image[]> {
		@Override
		public Image[] deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
			ObjectMapper mapper = new ObjectMapper();
			mapper.setDeserializationConfig(ctxt.getConfig());
			jp.setCodec(mapper);
			if (jp.hasCurrentToken()) {
				JsonNode dataNode = jp.readValueAsTree();
				if (dataNode != null) {
					return (Image[]) mapper.readValue(dataNode,new TypeReference<Image[]>(){});
				}
			}
			return null;
		}
	}
}
